package com.javasm.service.goodsService.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.javasm.common.page.PageResult;
import com.javasm.domin.criteria.goodsCriteria.GoodsCriteria;
import com.javasm.domin.entity.goodsEntity.GdGoods;
import com.javasm.domin.vo.goodsVo.GoodsVo;
import com.javasm.mapper.goodsMapper.GdBrandMapper;
import com.javasm.mapper.goodsMapper.GdColourMapper;
import com.javasm.mapper.goodsMapper.GdGoodsMapper;
import com.javasm.mapper.goodsMapper.GdTypeMapper;
import com.javasm.service.goodsService.GdGoodsService;
import com.javasm.transfer.goodsTransfer.GdGoodsTransfer;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
* @author Administrator
* @description 针对表【gd_goods】的数据库操作Service实现
* @createDate 2021-11-18 21:42:58
*/
@Service
@Transactional
@RequiredArgsConstructor
public class GdGoodsServiceImpl extends ServiceImpl<GdGoodsMapper, GdGoods> implements GdGoodsService {


    @Autowired
    private GdGoodsMapper gdGoodsMapper;
    @Autowired
    private GdTypeMapper gdTypeMapper;
    @Autowired
    private GdBrandMapper gdBrandMapper;
    @Autowired
    private GdColourMapper gdColourMapper;

    @Autowired
    private GdGoodsTransfer goodsTransfer;

    @Override
    public PageResult<GoodsVo> searchPage(GoodsCriteria goodsCriteria) {
        String goodsName = goodsCriteria.getGoodsName();
        Integer brandId = goodsCriteria.getBrandId();
        Integer colourId = goodsCriteria.getColourId();
        Integer typeId = goodsCriteria.getTypeId();
        String modelId = goodsCriteria.getModelId();
        LambdaQueryWrapper<GdGoods> lambda = new QueryWrapper<GdGoods>().lambda();

        if (!StringUtils.isEmpty(goodsName)){
            lambda.like(GdGoods::getGoodsName,goodsName);
        }
        if (!StringUtils.isEmpty(brandId)){
            lambda.eq(GdGoods::getBrandId,brandId);
        }
        if (!StringUtils.isEmpty(colourId)){
            lambda.eq(GdGoods::getColourId,colourId);
        }
        if (!StringUtils.isEmpty(typeId)){
            lambda.eq(GdGoods::getTypeId,typeId);
        }
        if (!StringUtils.isEmpty(modelId)){
            lambda.like(GdGoods::getModelId,modelId);
        }

        PageHelper.startPage(goodsCriteria.getCurrentPage(),goodsCriteria.getPageSize());

        List<GdGoods> gdGoodsList = gdGoodsMapper.selectList(lambda);

        PageInfo<GdGoods> gdGoodsPageInfo = new PageInfo<>(gdGoodsList);

        long total = gdGoodsPageInfo.getTotal();

        List<GoodsVo> set = goodsTransfer.set(gdGoodsList);

        return new PageResult<GoodsVo>(total,set);
    }

    @Override
    public int batchDeleteByIds(List<Long> ids) {
        return gdGoodsMapper.deleteBatchIds(ids);
    }

    @Override
    public int selectTypeId(Long id) {
        return gdGoodsMapper.selectTypeIdInt(id);
    }

    @Override
    public int updateByIdStock(Long id, Long stock) {
        return gdGoodsMapper.updateByIdStock(id,stock);
    }
}
